package com.Test;

import java.util.ArrayList;

public class SortDemo {
    public static void main(String[] args) {
        ArrayList<Process> arrayList = new ArrayList<>();
        arrayList.add(new Process(1L, "翻布"));
        arrayList.add(new Process(2L, "染色"));
        arrayList.add(new Process(3L, "预订"));
        arrayList.add(new Process(4L, "脱水"));
        arrayList.add(new Process(5L, "回定"));
        arrayList.add(new Process(6L, "扎布"));

        // 当前工序 ID
        long currentId = 2L;
        Process process = getPreviousProcess(arrayList, currentId);

        if (process != null) {
            System.out.println("上一道工序为:" + process.getName());
        } else {
            System.out.println("当前工序为第一道工序,没有上一道工序");
        }
    }

    private static Process getPreviousProcess(ArrayList<Process> arrayList, long currentId) {
        // 查找当前 ID 的索引
        int currentIndex = -1;
        for (int i = 0; i < arrayList.size(); i++) {
            if (arrayList.get(i).getId() == currentId) {
                currentIndex = i;
                break;
            }
        }

        // 如果当前 ID 找到了，返回前一个工序
        if (currentIndex > 0) {
            return arrayList.get(currentIndex - 1);
        } else {
            // 当前工序是第一个，前面没有工序
            return null;
        }
    }
}
